package com.kfzx.www.animationdemo;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class EnlargeButtonActivity extends AppCompatActivity implements View.OnClickListener {

    private Button mButton;
    private Button translatex, translatey, scalex, scaley, rotate, alpha;
    private ObjectAnimator translatexAnimator, translationYAnimator, scaleXAnimator, scaleYAnimator,
            rotateAnimator, alphaAnimator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enlarge_button);

        mButton = (Button) findViewById(R.id.enlarge_button);
//        ValueAnimator valueAnimator = ValueAnimator.ofInt(mButton.getLayoutParams().width, 700);
//        valueAnimator.setDuration(2000);
//        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
//        valueAnimator.setRepeatMode(ValueAnimator.RESTART);
//        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator valueAnimator) {
//                int currentValue = (Integer) valueAnimator.getAnimatedValue();
//                mButton.getLayoutParams().width = currentValue;
//                mButton.requestLayout();
//
//            }
//        });
//        valueAnimator.start();
//        Animator animator2 = AnimatorInflater.loadAnimator(this, R.animator.enlarge_button);
//        animator2.setTarget(mButton);
//        animator2.start();
//        animator2.addListener();


        translatex = (Button) findViewById(R.id.translatex);
        translatey = (Button) findViewById(R.id.translatey);
        scalex = (Button) findViewById(R.id.scalex);
        scaley = (Button) findViewById(R.id.scaley);
        rotate = (Button) findViewById(R.id.rotate);
        alpha = (Button) findViewById(R.id.alpha);
        

        translatex.setOnClickListener(this);
        translatey.setOnClickListener(this);
        scalex.setOnClickListener(this);
        scaley.setOnClickListener(this);
        rotate.setOnClickListener(this);
        alpha.setOnClickListener(this);


//        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mButton, "alpha", 1, 0);
//        objectAnimator.setDuration(2000);
//
//        objectAnimator.setRepeatCount(ObjectAnimator.INFINITE);
//        objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
//        objectAnimator.start();


    }

    @Override
    public void onClick(View view) {
        if (translatexAnimator != null && translationYAnimator != null && scaleXAnimator != null
                && scaleYAnimator != null && rotateAnimator != null && alphaAnimator != null) {
            cancelAnimator();

        }

        switch (view.getId()) {
            case R.id.translatex:
                translatexAnimator = ObjectAnimator.ofFloat(mButton, "translationX", 0, 500);
                translatexAnimator.setDuration(2000);
                translatexAnimator.setRepeatCount(ObjectAnimator.INFINITE);
                translatexAnimator.setRepeatMode(ObjectAnimator.REVERSE);

                translatexAnimator.start();
                break;
            case R.id.translatey:
//                translatexAnimator.cancel();
                translationYAnimator = ObjectAnimator.ofFloat(mButton, "translationY", 0, 500);
                translationYAnimator.setDuration(2000);
                translationYAnimator.setRepeatCount(ObjectAnimator.INFINITE);
                translationYAnimator.setRepeatMode(ObjectAnimator.REVERSE);
                translationYAnimator.start();
                break;

            case R.id.scalex:
                scaleXAnimator = ObjectAnimator.ofFloat(mButton, "scaleX", 1, 0);
                scaleXAnimator.setDuration(2000);
                scaleXAnimator.setRepeatCount(ObjectAnimator.INFINITE);
                scaleXAnimator.setRepeatMode(ObjectAnimator.REVERSE);
                scaleXAnimator.start();
                break;

            case R.id.scaley:
                scaleYAnimator = ObjectAnimator.ofFloat(mButton, "scaleY", 1, 3, 1);
                scaleYAnimator.setDuration(2000);
                scaleYAnimator.setRepeatCount(ObjectAnimator.INFINITE);
                scaleYAnimator.setRepeatMode(ObjectAnimator.REVERSE);
                scaleYAnimator.start();
                break;


            case R.id.rotate:
                rotateAnimator = ObjectAnimator.ofFloat(mButton, "rotation", 0, 360);
                rotateAnimator.setDuration(2000);
                rotateAnimator.setRepeatCount(ObjectAnimator.INFINITE);
                rotateAnimator.setRepeatMode(ObjectAnimator.REVERSE);
                rotateAnimator.start();
                break;


            case R.id.alpha:
                alphaAnimator = ObjectAnimator.ofFloat(mButton, "alpha", 1, 0);
                alphaAnimator.setDuration(2000);
                alphaAnimator.setRepeatCount(ObjectAnimator.INFINITE);
                alphaAnimator.setRepeatMode(ObjectAnimator.REVERSE);
                alphaAnimator.start();
                break;
        }
    }

    private void cancelAnimator() {
        translatexAnimator.cancel();
        translationYAnimator.cancel();
        scaleXAnimator.cancel();
        scaleYAnimator.cancel();
        rotateAnimator.cancel();
        alphaAnimator.cancel();
    }
}
